﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Anuntiomatic.Server.ManageList
{
    public partial class AccountInfoManagment : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindDataGrid();
            pagingtop.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pagingtop.CurrentPage = 1;
                pagingtop.PageSize = PageSize = 50;
                pagingtop.PageDisplay = 6;
                
                for (int i = 1; i <= 30; i++)
                {
                    ddlExtension.Items.Add(i.ToString());
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        protected void grvSysUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Editing":
                    hdfObjectId.Value = (string)e.CommandArgument;
                    var obj = DB.AccountInfos.FirstOrDefault(p => p.Email.Equals(e.CommandArgument));
                    if (obj != null)
                    {
                        txtEmail.Text = obj.Email;
                        RegisterStartUpScript("OpenDialog('dialog-form', 400)");
                    }
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var obj = DB.AccountInfos.FirstOrDefault(p => p.Email == hdfObjectId.Value);
            if (obj != null)
            {
                obj.LicenseTo = (obj.LicenseTo >= DateTime.Now ? obj.LicenseTo : DateTime.Now)
                    .AddDays(int.Parse(ddlExtension.SelectedValue));
                DB.SubmitChanges();
                BindDataGrid();
            }
        }

        void BindDataGrid()
        {
            int currentPage = pagingtop.CurrentPage;
            int pageSize = pagingtop.PageSize;
            int totalRow = 0;

            string email = txtSearchEmail.Text.Trim();
            bool? expired = ddlSearchLicenseInfo.SelectedValue == "" ? (bool?)null : bool.Parse(ddlSearchLicenseInfo.SelectedValue);
            var query = DB.AccountInfos.AsQueryable();
            if (!string.IsNullOrEmpty(email))
                query = query.Where(p => p.Email == email);
            if (expired != null)
            {
                if (expired == true)
                    query = query.Where(p => p.LicenseTo < DateTime.Now);
                else
                    query = query.Where(p => p.LicenseTo >= DateTime.Now);
            }
            totalRow = query.Count();

            grvSysUser.DataSource = query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
            grvSysUser.DataBind();
           
            pagingtop.TotalRecord =  totalRow;
            pagingtop.Visible = pagingtop.TotalPage > 1;
            ltrInfo.Text = "Total: " + pagingtop.TotalPage + " pages /" + totalRow + " records";
        }
    }
}